package com.mapfinal.mapcache.offline;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.lambkit.core.config.ConfigManager;
import com.lambkit.web.render.BufferedImageRender;
import com.mapfinal.mapcache.MapCachePersistence;
import com.mapfinal.mapcache.MapServer;
import com.mapfinal.mapcache.MapServerManager;
import com.mapfinal.mapcache.ParameterConversion;
import com.mapfinal.mapcache.cache.OfflineFileCachePersistence;
import com.mapfinal.server.service.tile.TileParam;

public class OfflineMapServer implements MapServer {
	
	private OfflineFileCachePersistence mapCache;
	private ParameterConversion parameterConversion;
	private Map<String, OfflineMapConfig> serverConfig;
	
	public static final String OFFLINE_DEFAULT_NAME = "offline";
	private static final String OFFLINE_MAP_PREFIX = "lambkit.mapcache.offline.";
	
	public OfflineMapServer() {
		// TODO Auto-generated constructor stub
		mapCache = new OfflineFileCachePersistence();
		serverConfig = new HashMap<String, OfflineMapConfig>();
		parameterConversion = new OfflineParameterConversion();
	}
	
	public OfflineMapServer(String name, String path) {
		// TODO Auto-generated constructor stub
		mapCache = new OfflineFileCachePersistence();
		serverConfig = new HashMap<String, OfflineMapConfig>();
		serverConfig.put(name, new OfflineMapConfig(name, path));
		parameterConversion = new OfflineParameterConversion();
	}
	
	/**
	 * 加载配置
	 */
	public void initServerConfig() {
		Properties prop = ConfigManager.me().getProperties();
		Set<String> datasourceNames = new HashSet<>();
		for (Map.Entry<Object, Object> entry : prop.entrySet()) {
			String key = entry.getKey().toString();
			if (key.startsWith(OFFLINE_MAP_PREFIX) && entry.getValue() != null) {
				String[] keySplits = key.split("\\.");
				if (keySplits.length == 5) {
					datasourceNames.add(keySplits[3]);
				}
			}
		}

		for (String name : datasourceNames) {
			OfflineMapConfig dsc = ConfigManager.me().get(OfflineMapConfig.class, OFFLINE_MAP_PREFIX + name);
			dsc.setName(name);
			if (dsc.isConfigOk()) {
				serverConfig.put(name, dsc);
			}
		}
	}
	
	public void putServerConfig(String name, String path) {
		serverConfig.put(name, new OfflineMapConfig(name, path));
	}

	@Override
	public void renderTileImage(TileParam tile, Controller controller) {
		// TODO Auto-generated method stub
		String server = tile.getServer();
		if(StrKit.notBlank(server)) {
			OfflineMapConfig dsc = serverConfig.get(server);
			String pathTemplate = "/{z}/{x}/{y}.{suffix}";
			if(dsc!=null) {
				pathTemplate = dsc.getPath();
			}
			mapCache.setPathTemplate(pathTemplate);
			boolean flag = getMapCache().renderFromCache(tile, controller);
			if(flag) {
				return;
			}
		} else {
			controller.render(new BufferedImageRender(MapServerManager.me().getEmptyImage(), "png"));
			System.out.println("map invalid");
			return;
		}
		controller.render(new BufferedImageRender(MapServerManager.me().getEmptyImage(), "png"));
		System.out.println("online not support");
	}

	@Override
	public MapCachePersistence getMapCache() {
		// TODO Auto-generated method stub
		return mapCache;
	}

	@Override
	public void setMapCache(MapCachePersistence mapCache) {
		// TODO Auto-generated method stub
		this.mapCache = (OfflineFileCachePersistence) mapCache;
	}

	@Override
	public ParameterConversion getParameterConversion() {
		// TODO Auto-generated method stub
		return parameterConversion;
	}

	@Override
	public void setParameterConversion(ParameterConversion parameterConversion) {
		// TODO Auto-generated method stub
		this.parameterConversion = parameterConversion;
	}
	
	public Map<String, OfflineMapConfig> getServerConfig() {
		return serverConfig;
	}

}
